﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace QLKS.Models
{
    public class TinNhanModel
    {
        //Lấy tất cả tin nhấn đến
        static public DataTable GetAllMessageTo(string username)
        {
            return DataProviderModel.QueryResults(@"SELECT * from dmTinNhan WHERE '" + username + "' LIKE nguoinhan AND trangthai <> 2").Tables[0];
        }

        //Lấy tất cả tin nhấn đã gửi
        static public DataTable GetAllMessageSend(string username)
        {
            return DataProviderModel.QueryResults(@"SELECT * from dmTinNhan WHERE nguoigui = '" + username + "'").Tables[0];
        }

        //Lấy tất cả tin nhấn đã xóa
        static public DataTable GetAllMessageDelete(string username)
        {
            return DataProviderModel.QueryResults(@"SELECT * from dmTinNhan WHERE '" + username + "' LIKE nguoinhan AND trangthai = 2").Tables[0];
        }

        //Set trạng thái
        static public void SetTrangThai(int matinnhan,int trangthai)
        {
            DataProviderModel.RunExecute("Update dmTinNhan Set trangthai = " + trangthai + " WHERE matinnhan = " + matinnhan);
        }

        //Tạo mới TinNhan
        static public DataTable CreateNewTinNhan(string nguoigui)
        {
            DataTable tb = DataProviderModel.QueryResults("SELECT * FROM dmTinNhan WHERE 1=0").Tables[0];
            DataRow row = tb.NewRow();
            row["tieude"] = "";
            row["noidung"] = "";
            row["nguoinhan"] = "";
            row["ngay"] = DateTime.Now.Date;
            row["nguoigui"] = nguoigui;
            row["trangthai"] = 0;
            tb.Rows.Add(row);
            return tb;
        }

        //Gửi tin nhấn
        static public int GuiTinNhan(DataTable tb)
        {
            return DataProviderModel.Insert(tb, "dmTinNhan", "matinnhan");
        }
    }
}
